package cn.ilikes.tools.nosql.jpa.springs;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.spi.PersistenceUnitInfo;

import org.slf4j.Logger;

import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.nosql.jpa.configs.JPASQLConfigurationPaserException;
import cn.ilikes.tools.nosql.jpa.configs.JPASQLPersistenceUnitMetadata;
import cn.ilikes.tools.nosql.jpa.configs.PersistenceXMLLoader;
import cn.ilikes.tools.nosql.jpa.configs.envs.JPASQLConfigurationException;

public class SpringPersistenceUnitProcessor {

	private Logger logger = Loggerfactory.instance(SpringPersistenceUnitProcessor.class);

	private Map<String, PersistenceUnitInfo> persistenceUnitMetadatas = new HashMap<String, PersistenceUnitInfo>();

	public Map<String, PersistenceUnitInfo> processor(String[] persistenceXmlLocations) throws JPASQLConfigurationException {
		PersistenceXMLLoader persistenceXMLLoader = new PersistenceXMLLoader();
		try {
			for (String persistenceXmlLocation : persistenceXmlLocations) {
				Map<String, JPASQLPersistenceUnitMetadata> result = persistenceXMLLoader.paserPersistenceMetadatas(persistenceXmlLocation);
				persistenceUnitMetadatas.putAll(result);
			}
			return persistenceUnitMetadatas;
		} catch (JPASQLConfigurationPaserException e) {
			Loggerfactory.error(logger, e);
			throw new JPASQLConfigurationException(e);
		}
	}

	public PersistenceUnitInfo selectPersistenceUnitInfo(String persistenceUnitInfoName) {
		return persistenceUnitMetadatas.get(persistenceUnitInfoName);
	}

}
